<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car_structure_model extends CI_Model {

 
    function __construct()
    {
         parent::__construct();
    }

	function select_list()
	{
		 $this->db->from('base_carstructure');
		 $this->db->order_by("structurename", "asc"); 


		$query = $this->db->get();

		$result =$query->result_array();

		return $result;
	}
	function select_page($data = array(
			'structurename'=>''  //品牌名称
			,'start'=>0		//开始记录数
			,'length'=>0))	//长度
	{
		if($data['structurename'])
		{
			$this->db->like('structurename',$data['structurename']);
		}

	    $this->db->from('base_carstructure');

		//查询总记录数
		$count = $this->db->count_all_results();

		$result['count'] = $count;
		/////////////////////////////////////////////////////////////////
		if($data['structurename'])
		{
			$this->db->like('structurename',$data['structurename']);
		}

	    $this->db->from('base_carstructure');

		if(intval($data['length'])<0)
		{
			$data['length'] =10;
		}
		if($data['start'])
		{
			$this->db->limit($data['length'],$data['start']);
		}
		else
		{
			$this->db->limit($data['length'],0);
		}
		$this->db->order_by("structurename", "asc"); 


		$query = $this->db->get();

		$result['list']=$query->result_array();

		return $result;
	}


	function select_by_id($structureid)
	{
		$this->db->where('structureid',$structureid);
		$query = $this->db->get('base_carstructure');
		$row = $query->row_array();
		return $row;
	}

	function update($structureid,$data=array())
	{
		$fields = array();
		if($data['structurename'])
		{
			$fields['structurename'] = $data['structurename'];
		}
		 
		$this->db->where('structureid', $structureid);
		return $this->db->update('base_carstructure', $fields); 
	}

	function insert($data=array())
	{
		$fields = array();
		if($data['structurename'])
		{
			$fields['structurename'] = $data['structurename'];
		}
	 
		$result = $this->db->insert('base_carstructure', $fields); 

		return $result;
	}

	function delete($structureid)
	{
		$result =array('success'=>true);
 
		$this->db->where('structureid',$structureid);

	    $this->db->from('base_carinfo');

		//查询总记录数
		$count = $this->db->count_all_results();

		if($count >0 )
		{
			$result['success']=false;
			$result['message'] ='删除车辆结构信息失败，车辆结构已在使用，请先删除对应的车辆信息！';
			return $result;
		}

		$this->db->where('structureid',$structureid);
		$result['data'] = $this->db->delete('base_carstructure'); 
		return $result;
	}
}